package com.ctshk.rpc.bus.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.enums.bus.SaleableType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.BConst;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.bus.dto.BusProductChannelOfflineAgentCustomDTO;
import com.ctshk.rpc.bus.dto.BusProductChannelOfflineSelfCustomDTO;
import com.ctshk.rpc.bus.dto.BusProductChannelOnlineSelfPlatformDTO;
import com.ctshk.rpc.bus.dto.BusProductChannelOnlineThirdPlatformDTO;
import com.ctshk.rpc.bus.dto.BusProductDetailDTO;
import com.ctshk.rpc.bus.dto.BusProductDetailRuleDTO;
import com.ctshk.rpc.bus.dto.BusProductIncreaseStrategyRuleDTO;
import com.ctshk.rpc.bus.dto.BusProductManagePageDTO;
import com.ctshk.rpc.bus.dto.BusProductStrategyDTO;
import com.ctshk.rpc.bus.dto.BusProductStrategyDateDTO;
import com.ctshk.rpc.bus.dto.Constants;
import com.ctshk.rpc.bus.entity.BusLine;
import com.ctshk.rpc.bus.entity.BusProduct;
import com.ctshk.rpc.bus.entity.BusProductChannelOfflineAgentCustom;
import com.ctshk.rpc.bus.entity.BusProductChannelOfflineSelfCustom;
import com.ctshk.rpc.bus.entity.BusProductChannelOnlineSelfPlatform;
import com.ctshk.rpc.bus.entity.BusProductChannelOnlineThirdPlatform;
import com.ctshk.rpc.bus.entity.BusProductIncreaseStrategyRule;
import com.ctshk.rpc.bus.entity.BusProductReservationSettings;
import com.ctshk.rpc.bus.entity.BusProductStrategy;
import com.ctshk.rpc.bus.entity.BusProductStrategyDate;
import com.ctshk.rpc.bus.mapper.BusLineMapper;
import com.ctshk.rpc.bus.mapper.BusProductChannelOfflineAgentCustomMapper;
import com.ctshk.rpc.bus.mapper.BusProductChannelOfflineSelfCustomMapper;
import com.ctshk.rpc.bus.mapper.BusProductChannelOnlineSelfPlatformMapper;
import com.ctshk.rpc.bus.mapper.BusProductChannelOnlineThirdPlatformMapper;
import com.ctshk.rpc.bus.mapper.BusProductIncreaseStrategyRuleMapper;
import com.ctshk.rpc.bus.mapper.BusProductMapper;
import com.ctshk.rpc.bus.mapper.BusProductReservationSettingsMapper;
import com.ctshk.rpc.bus.mapper.BusProductSoldMapper;
import com.ctshk.rpc.bus.mapper.BusProductStrategyDateMapper;
import com.ctshk.rpc.bus.mapper.BusProductStrategyMapper;
import com.ctshk.rpc.bus.req.BusProductChannelOfflineAgentCustomReq;
import com.ctshk.rpc.bus.req.BusProductChannelOfflineSelfCustomReq;
import com.ctshk.rpc.bus.req.BusProductDetailByStrategyReq;
import com.ctshk.rpc.bus.req.BusProductQueryReq;
import com.ctshk.rpc.bus.req.BusProductStrategyDateReq;
import com.ctshk.rpc.bus.req.BusProductStrategyReq;
import com.ctshk.rpc.bus.req.BusProductStrategySaveBatchReq;
import com.ctshk.rpc.bus.service.IBusProductStrategyService;
import com.ctshk.rpc.system.service.ISysPermissionService;

import cn.hutool.core.collection.CollectionUtil;

/**
 * <p>
 * 巴士产品加幅策略 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-19
 */
@DubboService
public class BusProductStrategyServiceImpl extends ServiceImpl<BusProductStrategyMapper, BusProductStrategy> implements IBusProductStrategyService, BaseService {

    @Autowired
    private BusProductMapper busProductMapper;
    @Autowired
    private BusProductStrategyMapper busProductStrategyMapper;
    @Autowired
    private BusProductStrategyDateMapper busProductStrategyDateMapper;
    @Autowired
    private BusProductChannelOfflineAgentCustomMapper busProductChannelOfflineAgentCustomMapper;
    @Autowired
    private BusProductIncreaseStrategyRuleMapper busProductIncreaseStrategyRuleMapper;
    @Autowired
    private BusProductChannelOfflineSelfCustomMapper busProductChannelOfflineSelfCustomMapper;
    @Autowired
    private BusProductChannelOnlineSelfPlatformMapper busProductChannelOnlineSelfPlatformMapper;
    @Autowired
    private BusProductChannelOnlineThirdPlatformMapper busProductChannelOnlineThirdPlatformMapper;
    @Autowired
    private BusProductReservationSettingsMapper busProductReservationSettingsMapper;
    @Autowired
    private BusLineMapper busLineMapper;
    
    @Autowired
    BusProductSoldMapper busProductSoldMapper;
    
    @Autowired
    ISysPermissionService sysPermissionService;

    /**
     * 产品中心-分页查询巴士产品列表
     * @param req
     * @return
     */
    @Override
    public Result<PageResponse<BusProductManagePageDTO>> queryList(BusProductQueryReq req, TokenUser tokenUser) {
    	//临时做法，默认填充菜单ID
    	if (req.getMenuId() == null) {
    		req.setMenuId(36135169530265600l);
    	}
    	//数据权限
    	List<Long> permissionUserList = sysPermissionService.queryPermission(req.getMenuId(), getTokenUserId(tokenUser)).ok();
    	
    	IPage<BusProduct> page = busProductMapper.queryApproveProductList(req, permissionUserList);
        List<BusProductManagePageDTO> list = Assist.toBeanList(page.getRecords(), BusProductManagePageDTO.class);
        
        Assist.forEach(list, dto -> {
        	if (dto.getLineId() != null) {
        		BusLine busLine = busLineMapper.selectById(dto.getLineId());
        		if (busLine != null) {
        			dto.setGatewayName(busLine.getGatewayName());
        		}
        	}
        });
        
        return newPageResult(page, list);
//        IPage<BusProductManagePageDTO> agentPage = new Page<>(req.getPageNo(),req.getPageSize());
//        agentPage = busProductStrategyMapper.queryList(req,agentPage);
//        PageResponse<BusProductManagePageDTO> result = new PageResponse<>(agentPage.getRecords(),agentPage.getCurrent() == 1,
//                agentPage.getTotal() <= (agentPage.getCurrent() * agentPage.getSize()),
//                agentPage.getTotal(), agentPage.getCurrent(), agentPage.getSize());
//        return result;
    }


    /** 巴士上架设置 */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result save(Long busProductId, List<BusProductStrategyReq> busProductStrategyReqList, Long userId) {
        // 校验
        Result checkResult = checkStrategyDateResult(busProductStrategyReqList, false);
        if (null != checkResult) {
            return checkResult;
        }

        // 插入加幅策略列表
        List<BusProductStrategy> strategyInsertList = new ArrayList<>();
        List<BusProductStrategyDate> strategyDateInsertList = new ArrayList<>();
        List<BusProductIncreaseStrategyRule> strategyRuleInsertList = new ArrayList<>();
        List<BusProductChannelOfflineSelfCustom> offlineSelfCustomInsertList = new ArrayList<>();
        List<BusProductChannelOfflineAgentCustom> offlineAgentCustomInsertList = new ArrayList<>();
        List<BusProductChannelOnlineSelfPlatform> onlineSelfPlatformInsertList = new ArrayList<>();
        List<BusProductChannelOnlineThirdPlatform> onlineThirdPlatformInsertList = new ArrayList<>();
        // 更新加幅策略列表
        List<BusProductStrategy> strategyUpdateList = new ArrayList<>();
        List<BusProductStrategyDate> strategyDateUpdateList = new ArrayList<>();
        List<BusProductIncreaseStrategyRule> strategyRuleUpdateList = new ArrayList<>();
        List<BusProductChannelOfflineSelfCustom> offlineSelfCustomUpdateList = new ArrayList<>();
        List<BusProductChannelOfflineAgentCustom> offlineAgentCustomUpdateList = new ArrayList<>();
        List<BusProductChannelOnlineSelfPlatform> onlineSelfPlatformUpdateList = new ArrayList<>();
        List<BusProductChannelOnlineThirdPlatform> onlineThirdPlatformUpdateList = new ArrayList<>();
        // 不需删除加幅策略ID列表
        List<Long> strategyIdList = new ArrayList<>();
        List<Long> strategyDateIdList = new ArrayList<>();
        List<Long> strategyRuleIdList = new ArrayList<>();
        List<Long> offlineSelfCustomIdList = new ArrayList<>();
        List<Long> offlineAgentCustomIdList = new ArrayList<>();
        List<Long> onlineSelfPlatformIdList = new ArrayList<>();
        List<Long> onlineThirdPlatformIdList = new ArrayList<>();
        LocalDateTime nowDateTime = LocalDateTime.now();

        // 查询当前机场快线-策略IDList
        List<Long> deleteStrategyIdList = busProductStrategyMapper.selectIdListByBusProductIds(Arrays.asList(busProductId));
        for (BusProductStrategyReq strategyReq : busProductStrategyReqList) {
            // 构建加幅策略
            BusProductStrategy busProductStrategy = EntityUtil.copy(strategyReq, BusProductStrategy.class);
            if (busProductStrategy.getId() == null) {
                busProductStrategy.setId(SnowflakeIdWorker.nextId());
                strategyInsertList.add(busProductStrategy);
            } else {
                // 剔除传过来的策略ID，剩下的是需要删除的
                if (deleteStrategyIdList.contains(busProductStrategy.getId())) {
                    deleteStrategyIdList.remove(busProductStrategy.getId());
                }
                strategyUpdateList.add(busProductStrategy);
                strategyIdList.add(busProductStrategy.getId());
            }
            busProductStrategy.setBusProductId(busProductId);
            // 构建加幅策略日期段
            List<BusProductStrategyDate> increaseStrategyDateList = EntityUtil.copyList(strategyReq.getBusProductStrategyDateReqList(), BusProductStrategyDate.class);
            for (BusProductStrategyDate strategyDate : increaseStrategyDateList) {
                if (strategyDate.getId() == null) {
                    strategyDate.setId(SnowflakeIdWorker.nextId());
                    strategyDateInsertList.add(strategyDate);
                } else {
                    strategyDateUpdateList.add(strategyDate);
                    strategyDateIdList.add(strategyDate.getId());
                }
                strategyDate.setIncreaseStrategyId(busProductStrategy.getId());
            }
            // 构建加幅策略-线下自营规则-所有
            if (busProductStrategy.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(strategyReq.getBusProductIncreaseStrategySelfRule(), BusProductIncreaseStrategyRule.class);
                if (increaseStrategyRule.getId() == null) {
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    strategyRuleInsertList.add(increaseStrategyRule);
                } else {
                    strategyRuleUpdateList.add(increaseStrategyRule);
                    strategyRuleIdList.add(increaseStrategyRule.getId());
                }
                increaseStrategyRule.setIncreaseStrategyId(busProductStrategy.getId());
                increaseStrategyRule.setSoldNumber(null);
                busProductStrategy.setSelfSalePermissionId(increaseStrategyRule.getIncreaseStrategyId());
            }
            // 构建加幅策略-线下自营规则-指定
            if (strategyReq.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && CollectionUtil.isNotEmpty(strategyReq.getBusProductChannelOfflineSelfCustomReqList())) {
                strategyReq.getBusProductChannelOfflineSelfCustomReqList().forEach(channelOfflineSelfCustomReq -> {
                    BusProductChannelOfflineSelfCustom channelOfflineSelfCustom = EntityUtil.copy(channelOfflineSelfCustomReq, BusProductChannelOfflineSelfCustom.class);
                    if (channelOfflineSelfCustom.getId() == null) {
                        channelOfflineSelfCustom.setId(SnowflakeIdWorker.nextId());
                        offlineSelfCustomInsertList.add(channelOfflineSelfCustom);
                    } else {
                        offlineSelfCustomUpdateList.add(channelOfflineSelfCustom);
                        offlineSelfCustomIdList.add(channelOfflineSelfCustom.getId());
                    }
                    channelOfflineSelfCustom.setIncreaseStrategyId(busProductStrategy.getId());
                    channelOfflineSelfCustom.setCreateId(userId);
                    channelOfflineSelfCustom.setGmtCreate(nowDateTime);
                    channelOfflineSelfCustom.setModifiedId(userId);
                    channelOfflineSelfCustom.setGmtModified(nowDateTime);
                    channelOfflineSelfCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineSelfCustomReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(busProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOfflineSelfCustom.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 构建加幅策略-线下代理规则-所有
            if (busProductStrategy.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(strategyReq.getBusProductIncreaseStrategyAgentRule(), BusProductIncreaseStrategyRule.class);
                if (increaseStrategyRule.getId() == null) {
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    strategyRuleInsertList.add(increaseStrategyRule);
                } else {
                    strategyRuleUpdateList.add(increaseStrategyRule);
                    strategyRuleIdList.add(increaseStrategyRule.getId());
                }
                increaseStrategyRule.setIncreaseStrategyId(busProductStrategy.getId());
                increaseStrategyRule.setSoldNumber(null);
                busProductStrategy.setSelfSalePermissionId(increaseStrategyRule.getIncreaseStrategyId());
            }
            // 构建加幅策略-线下代理规则-指定
            if (strategyReq.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyReq.getBusProductChannelOfflineAgentCustomReqList())) {
                strategyReq.getBusProductChannelOfflineAgentCustomReqList().forEach(channelOfflineAgentCustomReq -> {
                    BusProductChannelOfflineAgentCustom channelOfflineAgentCustom = EntityUtil.copy(channelOfflineAgentCustomReq, BusProductChannelOfflineAgentCustom.class);
                    if (channelOfflineAgentCustom.getId() == null) {
                        channelOfflineAgentCustom.setId(SnowflakeIdWorker.nextId());
                        offlineAgentCustomInsertList.add(channelOfflineAgentCustom);
                    } else {
                        offlineAgentCustomUpdateList.add(channelOfflineAgentCustom);
                        offlineAgentCustomIdList.add(channelOfflineAgentCustom.getId());
                    }
                    channelOfflineAgentCustom.setIncreaseStrategyId(busProductStrategy.getId());
                    channelOfflineAgentCustom.setCreateId(userId);
                    channelOfflineAgentCustom.setGmtCreate(nowDateTime);
                    channelOfflineAgentCustom.setModifiedId(userId);
                    channelOfflineAgentCustom.setGmtModified(nowDateTime);
                    channelOfflineAgentCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineAgentCustomReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(busProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOfflineAgentCustom.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 线上自营
            if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyReq.getBusProductChannelOnlineSelfPlatformReqList())) {
                strategyReq.getBusProductChannelOnlineSelfPlatformReqList().forEach(channelOnlineSelfPlatformReq -> {
                    BusProductChannelOnlineSelfPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineSelfPlatformReq, BusProductChannelOnlineSelfPlatform.class);
                    if (channelOnlineSelfPlatform.getId() == null) {
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        onlineSelfPlatformInsertList.add(channelOnlineSelfPlatform);
                    } else {
                        onlineSelfPlatformUpdateList.add(channelOnlineSelfPlatform);
                        onlineSelfPlatformIdList.add(channelOnlineSelfPlatform.getId());
                    }
                    channelOnlineSelfPlatform.setIncreaseStrategyId(busProductStrategy.getId());
                    channelOnlineSelfPlatform.setCreateId(userId);
                    channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                    channelOnlineSelfPlatform.setModifiedId(userId);
                    channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                    channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineSelfPlatformReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(busProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 线上第三方
            if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyReq.getBusProductChannelOnlineThirdPlatformReqList())) {
                strategyReq.getBusProductChannelOnlineThirdPlatformReqList().forEach(channelOnlineThirdPlatformReq -> {
                    BusProductChannelOnlineThirdPlatform channelOnlineThirdPlatform = EntityUtil.copy(channelOnlineThirdPlatformReq, BusProductChannelOnlineThirdPlatform.class);
                    if (channelOnlineThirdPlatform.getId() == null) {
                        channelOnlineThirdPlatform.setId(SnowflakeIdWorker.nextId());
                        onlineThirdPlatformInsertList.add(channelOnlineThirdPlatform);
                    } else {
                        onlineThirdPlatformUpdateList.add(channelOnlineThirdPlatform);
                        onlineThirdPlatformIdList.add(channelOnlineThirdPlatform.getId());
                    }
                    channelOnlineThirdPlatform.setIncreaseStrategyId(busProductStrategy.getId());
                    channelOnlineThirdPlatform.setCreateId(userId);
                    channelOnlineThirdPlatform.setGmtCreate(nowDateTime);
                    channelOnlineThirdPlatform.setModifiedId(userId);
                    channelOnlineThirdPlatform.setGmtModified(nowDateTime);
                    channelOnlineThirdPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineThirdPlatformReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(busProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOnlineThirdPlatform.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
        }

        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(deleteStrategyIdList)) {// 删除入参中不存在的ID列表
            busProductStrategyMapper.delete(new QueryWrapper<BusProductStrategy>().in("id", deleteStrategyIdList));
            busProductStrategyDateMapper.delete(new QueryWrapper<BusProductStrategyDate>().in("increase_strategy_id", deleteStrategyIdList));
            busProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<BusProductIncreaseStrategyRule>().in("increase_strategy_id", deleteStrategyIdList));
            busProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineSelfCustom>().in("increase_strategy_id", deleteStrategyIdList));
            busProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineAgentCustom>().in("increase_strategy_id", deleteStrategyIdList));
            busProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineSelfPlatform>().in("increase_strategy_id", deleteStrategyIdList));
            busProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineThirdPlatform>().in("increase_strategy_id", deleteStrategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyDateIdList)) {// 删除不存在的ID列表
            busProductStrategyDateMapper.delete(new QueryWrapper<BusProductStrategyDate>().notIn("id", strategyDateIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {// 删除不存在的ID列表
            busProductStrategyDateMapper.delete(new QueryWrapper<BusProductStrategyDate>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyRuleIdList)) {
            busProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<BusProductIncreaseStrategyRule>().notIn("id", strategyRuleIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            busProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<BusProductIncreaseStrategyRule>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(offlineSelfCustomIdList)) {
            busProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineSelfCustom>().notIn("id", offlineSelfCustomIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            busProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineSelfCustom>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(offlineAgentCustomIdList)) {
            busProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineAgentCustom>().notIn("id", offlineAgentCustomIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            busProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineAgentCustom>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(onlineSelfPlatformIdList)) {
            busProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineSelfPlatform>().notIn("id", onlineSelfPlatformIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            busProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineSelfPlatform>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(onlineThirdPlatformIdList)) {
            busProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineThirdPlatform>().notIn("id", onlineThirdPlatformIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            busProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineThirdPlatform>().in("increase_strategy_id", strategyIdList));
        }
        // 更新
        updateBatchSomeColumn(strategyUpdateList, busProductStrategyMapper);
        updateBatchSomeColumn(strategyDateUpdateList, busProductStrategyDateMapper);
        updateBatchSomeColumn(strategyRuleUpdateList, busProductIncreaseStrategyRuleMapper);
        updateBatchSomeColumn(offlineSelfCustomUpdateList, busProductChannelOfflineSelfCustomMapper);
        updateBatchSomeColumn(offlineAgentCustomUpdateList, busProductChannelOfflineAgentCustomMapper);
        updateBatchSomeColumn(onlineSelfPlatformUpdateList, busProductChannelOnlineSelfPlatformMapper);
        updateBatchSomeColumn(onlineThirdPlatformUpdateList, busProductChannelOnlineThirdPlatformMapper);
        // 插入
        insertBatchSomeColumn(strategyInsertList, busProductStrategyMapper);
        insertBatchSomeColumn(strategyDateInsertList, busProductStrategyDateMapper);
        insertBatchSomeColumn(strategyRuleInsertList, busProductIncreaseStrategyRuleMapper);
        insertBatchSomeColumn(offlineSelfCustomInsertList, busProductChannelOfflineSelfCustomMapper);
        insertBatchSomeColumn(offlineAgentCustomInsertList, busProductChannelOfflineAgentCustomMapper);
        insertBatchSomeColumn(onlineSelfPlatformInsertList, busProductChannelOnlineSelfPlatformMapper);
        insertBatchSomeColumn(onlineThirdPlatformInsertList, busProductChannelOnlineThirdPlatformMapper);
        return Result.success();
    }

    /** 巴士产品批量加幅保存 */
    @Transactional
    @Override
    public Result saveBatch(BusProductStrategySaveBatchReq busProductStrategySaveBatchReq, Long userId) {
        List<Long> busProductIdList = busProductStrategySaveBatchReq.getBusProductIdList();
        List<BusProductStrategyReq> busProductStrategyReqList = busProductStrategySaveBatchReq.getBusProductStrategyReqList();
        // 校验
        Result checkResult = checkStrategyDateResult(busProductStrategyReqList, true);
        if (null != checkResult) {
            return checkResult;
        }
        LocalDateTime nowDateTime = LocalDateTime.now();
        // 删除加幅策略
        deleteStrategyByAirportExpressId(busProductIdList);
        // 插入加幅策略
        List<BusProductStrategy> busProductStrategyList = new ArrayList<>();
        List<BusProductIncreaseStrategyRule> busProductIncreaseStrategyRuleList = new ArrayList<>();
        List<BusProductStrategyDate> busProductStrategyDateList = new ArrayList<>();
        List<BusProductChannelOfflineAgentCustom> busProductChannelOfflineAgentCustomList = new ArrayList<>();
        List<BusProductChannelOfflineSelfCustom> busProductChannelOfflineSelfCustomList = new ArrayList<>();
        List<BusProductChannelOnlineSelfPlatform> busProductChannelOnlineSelfPlatformList = new ArrayList<>();
        List<BusProductChannelOnlineThirdPlatform> busProductChannelOnlineThirdPlatformList = new ArrayList<>();
        for (Long busProductId : busProductIdList) {
        	BusProduct busProduct = busProductMapper.assertExistById(busProductId);
        	
            for (BusProductStrategyReq increaseStrategyReq : busProductStrategyReqList) {
                // 构建加幅策略
                BusProductStrategy increaseStrategy = newCreateEntity(BusProductStrategy.class, increaseStrategyReq);;
                increaseStrategy.setBusProductId(busProductId);
                increaseStrategy.setId(SnowflakeIdWorker.nextId());
                // 构建加幅策略日期段
                List<BusProductStrategyDate> increaseStrategyDateList = EntityUtil.copyList(increaseStrategyReq.getBusProductStrategyDateReqList(), BusProductStrategyDate.class);
                for (BusProductStrategyDate strategyDate : increaseStrategyDateList) {
                    strategyDate.setId(SnowflakeIdWorker.nextId());
                    strategyDate.setIncreaseStrategyId(increaseStrategy.getId());
                }
                busProductStrategyDateList.addAll(increaseStrategyDateList);
                // 构建加幅策略-线下自营规则-所有
                if (increaseStrategy.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                    BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(increaseStrategyReq.getBusProductIncreaseStrategySelfRule(), BusProductIncreaseStrategyRule.class);
                    //验证设置的最大可售数是否已超过库存数
                    if (!validateSaleableNumber(increaseStrategyRule, busProduct.getInventory())) {
                    	return Result.failed(SystemError.BUS_ORDER_9021);
                    }

                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                    increaseStrategyRule.setSoldNumber(null);
                    increaseStrategy.setSelfSalePermissionId(increaseStrategyRule.getId());
                    busProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                }
                // 构建加幅策略-线下自营规则-指定
                if (increaseStrategyReq.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(increaseStrategyReq.getBusProductChannelOfflineSelfCustomReqList())) {
                	for (BusProductChannelOfflineSelfCustomReq channelOfflineSelfCustomReq : increaseStrategyReq.getBusProductChannelOfflineSelfCustomReqList()) {
                        BusProductChannelOfflineSelfCustom channelOfflineSelfCustom = EntityUtil.copy(channelOfflineSelfCustomReq, BusProductChannelOfflineSelfCustom.class);
                        channelOfflineSelfCustom.setId(SnowflakeIdWorker.nextId());
                        channelOfflineSelfCustom.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOfflineSelfCustom.setCreateId(userId);
                        channelOfflineSelfCustom.setGmtCreate(nowDateTime);
                        channelOfflineSelfCustom.setModifiedId(userId);
                        channelOfflineSelfCustom.setGmtModified(nowDateTime);
                        channelOfflineSelfCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                        busProductChannelOfflineSelfCustomList.add(channelOfflineSelfCustom);
                        
                        
                        // 插入加幅规则
                        BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineSelfCustomReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                        //验证设置的最大可售数是否已超过库存数
                        if (!validateSaleableNumber(increaseStrategyRule, busProduct.getInventory())) {
                        	return Result.failed(SystemError.BUS_ORDER_9021);
                        }
                        
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOfflineSelfCustom.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        busProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    };
                }
                // 构建加幅策略-线下代理规则-所有
                if (increaseStrategy.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                    BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(increaseStrategyReq.getBusProductIncreaseStrategyAgentRule(), BusProductIncreaseStrategyRule.class);
                    //验证设置的最大可售数是否已超过库存数
                    if (!validateSaleableNumber(increaseStrategyRule, busProduct.getInventory())) {
                    	return Result.failed(SystemError.BUS_ORDER_9021);
                    }

                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                    increaseStrategyRule.setSoldNumber(null);
                    increaseStrategy.setAgentSalePermissionId(increaseStrategyRule.getId());
                    busProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                }
                // 构建加幅策略-线下代理规则-指定
                if (increaseStrategyReq.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(increaseStrategyReq.getBusProductChannelOfflineAgentCustomReqList())) {
                    for (BusProductChannelOfflineAgentCustomReq channelOfflineAgentCustomReq : increaseStrategyReq.getBusProductChannelOfflineAgentCustomReqList()) {
                        BusProductChannelOfflineAgentCustom channelOfflineAgentCustom = EntityUtil.copy(channelOfflineAgentCustomReq, BusProductChannelOfflineAgentCustom.class);
                        channelOfflineAgentCustom.setId(SnowflakeIdWorker.nextId());
                        channelOfflineAgentCustom.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOfflineAgentCustom.setCreateId(userId);
                        channelOfflineAgentCustom.setGmtCreate(nowDateTime);
                        channelOfflineAgentCustom.setModifiedId(userId);
                        channelOfflineAgentCustom.setGmtModified(nowDateTime);
                        channelOfflineAgentCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                        busProductChannelOfflineAgentCustomList.add(channelOfflineAgentCustom);
                        // 插入加幅规则
                        BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineAgentCustomReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                        //验证设置的最大可售数是否已超过库存数
                        if (!validateSaleableNumber(increaseStrategyRule, busProduct.getInventory())) {
                        	return Result.failed(SystemError.BUS_ORDER_9021);
                        }
                        
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOfflineAgentCustom.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        busProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    }
                }
                // 线上自营
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(increaseStrategyReq.getBusProductChannelOnlineSelfPlatformReqList())) {
                    increaseStrategyReq.getBusProductChannelOnlineSelfPlatformReqList().forEach(channelOnlineSelfPlatformReq -> {
                        BusProductChannelOnlineSelfPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineSelfPlatformReq, BusProductChannelOnlineSelfPlatform.class);
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        channelOnlineSelfPlatform.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOnlineSelfPlatform.setCreateId(userId);
                        channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                        channelOnlineSelfPlatform.setModifiedId(userId);
                        channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                        channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                        busProductChannelOnlineSelfPlatformList.add(channelOnlineSelfPlatform);
                        // 插入加幅规则
                        BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineSelfPlatformReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                        //验证设置的最大可售数是否已超过库存数
                        validateSaleableNumber(increaseStrategyRule, busProduct.getInventory());
                        
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        busProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                // 线上第三方
                if (CollectionUtils.isNotEmpty(increaseStrategyReq.getBusProductChannelOnlineThirdPlatformReqList())) {
                    increaseStrategyReq.getBusProductChannelOnlineThirdPlatformReqList().forEach(channelOnlineThirdPlatformReq -> {
                        BusProductChannelOnlineThirdPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineThirdPlatformReq, BusProductChannelOnlineThirdPlatform.class);
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        channelOnlineSelfPlatform.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOnlineSelfPlatform.setCreateId(userId);
                        channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                        channelOnlineSelfPlatform.setModifiedId(userId);
                        channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                        channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                        busProductChannelOnlineThirdPlatformList.add(channelOnlineSelfPlatform);
                        // 插入加幅规则
                        BusProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineThirdPlatformReq.getBusProductIncreaseStrategyRuleReq(), BusProductIncreaseStrategyRule.class);
                        //验证设置的最大可售数是否已超过库存数
                        validateSaleableNumber(increaseStrategyRule, busProduct.getInventory());
                        
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        busProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                busProductStrategyList.add(increaseStrategy);
            }
            
            fillUpdateEntityUserId(busProduct, userId);
            busProduct.setOnShelfStatus(BConst.ONE);
            busProductMapper.updateById(busProduct);
        }
        insertBatchSomeColumn(busProductStrategyList, busProductStrategyMapper);
        insertBatchSomeColumn(busProductStrategyDateList, busProductStrategyDateMapper);
        insertBatchSomeColumn(busProductIncreaseStrategyRuleList, busProductIncreaseStrategyRuleMapper);
        insertBatchSomeColumn(busProductChannelOfflineAgentCustomList, busProductChannelOfflineAgentCustomMapper);
        insertBatchSomeColumn(busProductChannelOfflineSelfCustomList, busProductChannelOfflineSelfCustomMapper);
        insertBatchSomeColumn(busProductChannelOnlineSelfPlatformList, busProductChannelOnlineSelfPlatformMapper);
        insertBatchSomeColumn(busProductChannelOnlineThirdPlatformList, busProductChannelOnlineThirdPlatformMapper);
        return Result.success();
    }


    /**
     * 查询产品加幅信息
     * @param productId
     * @return
     */
    @Override
    public List<BusProductStrategyDTO> queryByProductId(Long productId) {

        // 查询加幅策略列表
        QueryWrapper<BusProductStrategy> aeIncreaseStrategyWrapper = new QueryWrapper<>();
        aeIncreaseStrategyWrapper.lambda().eq(BusProductStrategy::getBusProductId, productId);
        List<BusProductStrategy> busProductStrategyList = busProductStrategyMapper.selectList(aeIncreaseStrategyWrapper);
        List<BusProductStrategyDTO> busProductStrategyDTOS = EntityUtil.copyList(busProductStrategyList, BusProductStrategyDTO.class);
        for (BusProductStrategyDTO aeIncreaseStrategyDTO : busProductStrategyDTOS) {
            // 查询加幅策略-日期列表
            QueryWrapper<BusProductStrategyDate> aeIncreaseStrategyDateWrapper = new QueryWrapper<>();
            aeIncreaseStrategyDateWrapper.lambda().eq(BusProductStrategyDate::getIncreaseStrategyId, aeIncreaseStrategyDTO.getId());
            List<BusProductStrategyDate> aeIncreaseStrategyDates = busProductStrategyDateMapper.selectList(aeIncreaseStrategyDateWrapper);
            aeIncreaseStrategyDTO.setBusProductStrategyDateDTOList(EntityUtil.copyList(aeIncreaseStrategyDates, BusProductStrategyDateDTO.class));
            // 查询加幅策略-规则列表
            QueryWrapper<BusProductIncreaseStrategyRule> aeIncreaseStrategyRuleWrapper = new QueryWrapper<>();
            aeIncreaseStrategyRuleWrapper.lambda().eq(BusProductIncreaseStrategyRule::getIncreaseStrategyId, aeIncreaseStrategyDTO.getId());
            List<BusProductIncreaseStrategyRule> aeIncreaseStrategyRules = busProductIncreaseStrategyRuleMapper.selectList(aeIncreaseStrategyRuleWrapper);
            List<BusProductIncreaseStrategyRuleDTO> aeIncreaseStrategyRuleDTOs = EntityUtil.copyList(aeIncreaseStrategyRules, BusProductIncreaseStrategyRuleDTO.class);
            // 查询加幅策略-线下自营
            if (aeIncreaseStrategyDTO.getSelfSalePermissionType() != null) {
                if (Constants.SALE_CHANNEL_TYPE_ALL == aeIncreaseStrategyDTO.getSelfSalePermissionType()) {
                    List<BusProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getId().equals(aeIncreaseStrategyDTO.getSelfSalePermissionId())).collect(Collectors.toList());
                    aeIncreaseStrategyDTO.setBusIncreaseStrategySelfRule(Assist.findFirst(ruleList));
                }
                if (Constants.SALE_CHANNEL_TYPE_SELECTED == aeIncreaseStrategyDTO.getSelfSalePermissionType()) {
                    QueryWrapper<BusProductChannelOfflineSelfCustom> aeChannelOfflineSelfCustomWrapper = new QueryWrapper<>();
                    aeChannelOfflineSelfCustomWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
                    aeChannelOfflineSelfCustomWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                    List<BusProductChannelOfflineSelfCustom> aeChannelOfflineSelfCustoms = busProductChannelOfflineSelfCustomMapper.selectList(aeChannelOfflineSelfCustomWrapper);
                    if (CollectionUtils.isNotEmpty(aeChannelOfflineSelfCustoms)) {
                        List<BusProductChannelOfflineSelfCustomDTO> aeChannelOfflineSelfCustomDTOs = EntityUtil.copyList(aeChannelOfflineSelfCustoms, BusProductChannelOfflineSelfCustomDTO.class);
                        // 组装所属加幅规则
                        for (BusProductChannelOfflineSelfCustomDTO aeChannelOfflineSelfCustomDTO : aeChannelOfflineSelfCustomDTOs) {
                            List<BusProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeChannelOfflineSelfCustomDTO.getId().equals(ruleDTO.getCustomId())).collect(Collectors.toList());
                            aeChannelOfflineSelfCustomDTO.setBusProductIncreaseStrategyRuleDTO(Assist.findFirst(ruleList));
                        }
                        aeIncreaseStrategyDTO.setBusProductChannelOfflineSelfCustomDTOList(aeChannelOfflineSelfCustomDTOs);
                    }
                }
            }
            // 查询加幅策略-线下代理
            if (aeIncreaseStrategyDTO.getAgentSalePermissionType() != null) {
                if (Constants.SALE_CHANNEL_TYPE_ALL == aeIncreaseStrategyDTO.getAgentSalePermissionType()) {
                    List<BusProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getId().equals(aeIncreaseStrategyDTO.getAgentSalePermissionId())).collect(Collectors.toList());
                    aeIncreaseStrategyDTO.setBusIncreaseStrategyAgentRule(Assist.findFirst(ruleList));
                }
                if (Constants.SALE_CHANNEL_TYPE_SELECTED == aeIncreaseStrategyDTO.getAgentSalePermissionType()) {
                    QueryWrapper<BusProductChannelOfflineAgentCustom> aeChannelOfflineAgentCustomWrapper = new QueryWrapper<>();
                    aeChannelOfflineAgentCustomWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
                    aeChannelOfflineAgentCustomWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                    List<BusProductChannelOfflineAgentCustom> aeChannelOfflineAgentCustoms = busProductChannelOfflineAgentCustomMapper.selectList(aeChannelOfflineAgentCustomWrapper);
                    if (CollectionUtils.isNotEmpty(aeChannelOfflineAgentCustoms)) {
                        List<BusProductChannelOfflineAgentCustomDTO> aeChannelOfflineAgentCustomDTOs = EntityUtil.copyList(aeChannelOfflineAgentCustoms, BusProductChannelOfflineAgentCustomDTO.class);
                        // 组装所属加幅规则
                        for (BusProductChannelOfflineAgentCustomDTO aeChannelOfflineAgentCustomDTO : aeChannelOfflineAgentCustomDTOs) {
                            List<BusProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeChannelOfflineAgentCustomDTO.getId().equals(ruleDTO.getCustomId())).collect(Collectors.toList());
                            aeChannelOfflineAgentCustomDTO.setBusProductIncreaseStrategyRuleDTO(Assist.findFirst(ruleList));
                        }
                        aeIncreaseStrategyDTO.setBusProductChannelOfflineAgentCustomDTOList(aeChannelOfflineAgentCustomDTOs);
                    }
                }
            }
            // 查询加幅策略-线上自营
            QueryWrapper<BusProductChannelOnlineSelfPlatform> aeIncreaseChannelOnlineSelfPlatformWrapper = new QueryWrapper<>();
            aeIncreaseChannelOnlineSelfPlatformWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            aeIncreaseChannelOnlineSelfPlatformWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<BusProductChannelOnlineSelfPlatform> aeChannelOnlineSelfPlatforms = busProductChannelOnlineSelfPlatformMapper.selectList(aeIncreaseChannelOnlineSelfPlatformWrapper);
            if (CollectionUtils.isNotEmpty(aeChannelOnlineSelfPlatforms)) {
                List<BusProductChannelOnlineSelfPlatformDTO> aeChannelOnlineSelfPlatformDTOs = EntityUtil.copyList(aeChannelOnlineSelfPlatforms, BusProductChannelOnlineSelfPlatformDTO.class);
                // 组装所属加幅规则
                for (BusProductChannelOnlineSelfPlatformDTO aeChannelOnlineSelfPlatformDTO : aeChannelOnlineSelfPlatformDTOs) {
                    List<BusProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeChannelOnlineSelfPlatformDTO.getId().equals(ruleDTO.getCustomId())).collect(Collectors.toList());
                    aeChannelOnlineSelfPlatformDTO.setBusProductIncreaseStrategyRuleDTO(Assist.findFirst(ruleList));
                }
                aeIncreaseStrategyDTO.setBusProductChannelOnlineSelfPlatformDTOList(aeChannelOnlineSelfPlatformDTOs);
            }
            // 查询加幅策略-线上第三方
            QueryWrapper<BusProductChannelOnlineThirdPlatform> aeChannelOnlineThirdWrapper = new QueryWrapper<>();
            aeChannelOnlineThirdWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            aeChannelOnlineThirdWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<BusProductChannelOnlineThirdPlatform> aeChannelOnlineThirds = busProductChannelOnlineThirdPlatformMapper.selectList(aeChannelOnlineThirdWrapper);
            if (CollectionUtils.isNotEmpty(aeChannelOnlineThirds)) {
                List<BusProductChannelOnlineThirdPlatformDTO> aeChannelOnlineThirdPlatformDTOs = EntityUtil.copyList(aeChannelOnlineThirds, BusProductChannelOnlineThirdPlatformDTO.class);
                // 组装所属加幅规则
                for (BusProductChannelOnlineThirdPlatformDTO aeChannelOnlineThirdPlatformDTO : aeChannelOnlineThirdPlatformDTOs) {
                    List<BusProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeChannelOnlineThirdPlatformDTO.getId().equals(ruleDTO.getCustomId())).collect(Collectors.toList());
                    aeChannelOnlineThirdPlatformDTO.setBusProductIncreaseStrategyRuleDTO(Assist.findFirst(ruleList));
                }
                aeIncreaseStrategyDTO.setBusProductChannelOnlineThirdPlatformDTOList(aeChannelOnlineThirdPlatformDTOs);
            }
        }
        return busProductStrategyDTOS;





    }


    /**
     * 产品中心-查看详情
     * @param productId
     * @return
     */
    @Override
    public Result<BusProductDetailDTO> selectDetail(BusProductDetailByStrategyReq req) {
        BusProduct busProduct = busProductMapper.selectById(req.getProductId());
        BusProductDetailDTO busProductDetailDTO = EntityUtil.copy(busProduct, BusProductDetailDTO.class);

        BusProductReservationSettings busProductReservationSettings = busProductReservationSettingsMapper.selectOne(new QueryWrapper<BusProductReservationSettings>().lambda().eq(BusProductReservationSettings::getBusProductId,req.getProductId()));
        if (busProductReservationSettings != null) {
        	BeanUtils.copyProperties(busProductReservationSettings,busProductDetailDTO);
        }

        BusLine busLine = busLineMapper.selectById(busProduct.getLineId());
        if(!Objects.isNull(busLine)){
            busProductDetailDTO.setLineName(busLine.getLineName());
            busProductDetailDTO.setVehicleType(busLine.getVehicleType());
        }

        // 查询加幅策略列表
//        List<BusProductStrategy> busProductStrategyList = busProductStrategyMapper.queryListByValidDate(req.getProductId(), req.getBeginDate(), req.getBeginDate().plusMonths(1));
        List<BusProductStrategy> busProductStrategyList = busProductStrategyMapper.selectListByKey("bus_product_id", req.getProductId());
        TreeMap<LocalDate, BusProductDetailRuleDTO> dateRuleMap = buildAirportExpressList(busProductStrategyList, req.getBeginDate(), busProduct);
        busProductDetailDTO.setDateRuleInfoMap(dateRuleMap);
        return Result.success(busProductDetailDTO);
    }


    /**
     * 组装每天的加幅策略表
     *
     * @param strategyList
     * @return
     */
    private TreeMap<LocalDate, BusProductDetailRuleDTO> buildAirportExpressList(List<BusProductStrategy> strategyList, LocalDate beginDate
    		, BusProduct busProduct) {
    	LocalDate endDate = beginDate.plusMonths(1);
        TreeMap<LocalDate, BusProductDetailRuleDTO> localDateListTreeMap = new TreeMap<>(LocalDate::compareTo);
//        long diffMonthDay = beginDate.plusMonths(1).toEpochDay() - beginDate.toEpochDay();
//        for (int i = 0; i < diffMonthDay; i++) {
//        	localDateListTreeMap.put(beginDate.plusDays(i), new BusProductDetailRuleDTO());
//		}
        
        //已售信息列表
//        List<BusProductSold> soldList = busProductSoldMapper.selectListByKey("bus_product", busProduct.getId());
//        Map<Long, Map<Long, BigDecimal>>
        
        for (BusProductStrategy busProductStrategy : strategyList) {
            //线下加幅策略每日规则列表
            List<BusProductIncreaseStrategyRuleDTO> offlineRuleList = new ArrayList<>();
            //线上加幅策略每日规则列表
            List<BusProductIncreaseStrategyRuleDTO> onlineRuleList = new ArrayList<>();
            
            // 查询当前加幅策略-所有规则列表
            QueryWrapper<BusProductIncreaseStrategyRule> strategyRuleQueryWrapper = new QueryWrapper<>();
            strategyRuleQueryWrapper.eq("increase_strategy_id", busProductStrategy.getId());
            List<BusProductIncreaseStrategyRule> strategyRuleList = busProductIncreaseStrategyRuleMapper.selectList(strategyRuleQueryWrapper);
            // 加幅策略-所有线下自营
            if (Constants.SALE_CHANNEL_TYPE_ALL == busProductStrategy.getSelfSalePermissionType()) {
                List<BusProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(busProductStrategy.getSelfSalePermissionId(), rule.getId()));
//                List<BusProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> busProductStrategy.getSelfSalePermissionId().equals(rule.getId())).collect(Collectors.toList());
                if (Assist.isNotEmpty(ruleList)) {
                	BusProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), BusProductIncreaseStrategyRuleDTO.class);
                	strategyRuleInfoDTO.setChannelFullName("线下自营渠道-所有");
                	if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                		int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                		strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                	}
                	offlineRuleList.add(strategyRuleInfoDTO);
                }
            }
            // 加幅策略-所有线下代理
            if (Constants.SALE_CHANNEL_TYPE_ALL == busProductStrategy.getAgentSalePermissionType()) {
            	List<BusProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(busProductStrategy.getAgentSalePermissionId(), rule.getId()));
//                List<BusProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> busProductStrategy.getAgentSalePermissionId().equals(rule.getId())).collect(Collectors.toList());
                if (Assist.isNotEmpty(ruleList)) {
                	BusProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), BusProductIncreaseStrategyRuleDTO.class);
                	strategyRuleInfoDTO.setChannelFullName("线下代理渠道-所有");
                	if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                		int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                		strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                	}
                	offlineRuleList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-选择线下自营
            if (Constants.SALE_CHANNEL_TYPE_SELECTED == busProductStrategy.getSelfSalePermissionType()) {
                QueryWrapper<BusProductChannelOfflineSelfCustom> selfCustomQueryWrapper = new QueryWrapper<>();
                selfCustomQueryWrapper.eq("increase_strategy_id", busProductStrategy.getId());
                selfCustomQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<BusProductChannelOfflineSelfCustom> offlineSelfCustomList = busProductChannelOfflineSelfCustomMapper.selectList(selfCustomQueryWrapper);
                // 组装所属加幅规则
                for (BusProductChannelOfflineSelfCustom offlineSelfCustom : offlineSelfCustomList) {
                    List<BusProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(rule.getCustomId(), offlineSelfCustom.getId()));
                    BusProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), BusProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线下自营渠道-" + offlineSelfCustom.getName());
                    if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    offlineRuleList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-选择线下代理
            if (Constants.SALE_CHANNEL_TYPE_SELECTED == busProductStrategy.getAgentSalePermissionType()) {
                QueryWrapper<BusProductChannelOfflineAgentCustom> agentCustomQueryWrapper = new QueryWrapper<>();
                agentCustomQueryWrapper.eq("increase_strategy_id", busProductStrategy.getId());
                agentCustomQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<BusProductChannelOfflineAgentCustom> offlineAgentCustomList = busProductChannelOfflineAgentCustomMapper.selectList(agentCustomQueryWrapper);
                // 组装所属加幅规则
                for (BusProductChannelOfflineAgentCustom offlineAgentCustom : offlineAgentCustomList) {
                    List<BusProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(rule.getCustomId(), offlineAgentCustom.getId()));
                    BusProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), BusProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线下代理渠道-" + offlineAgentCustom.getAgentName());
                    if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    offlineRuleList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-线上自营
            QueryWrapper<BusProductChannelOnlineSelfPlatform> onlineSelfPlatformQueryWrapper = new QueryWrapper<>();
            onlineSelfPlatformQueryWrapper.eq("increase_strategy_id", busProductStrategy.getId());
            onlineSelfPlatformQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<BusProductChannelOnlineSelfPlatform> onlineSelfPlatformList = busProductChannelOnlineSelfPlatformMapper.selectList(onlineSelfPlatformQueryWrapper);
            // 查询加幅策略-线上自营-组装所属加幅规则
            for (BusProductChannelOnlineSelfPlatform onlineSelfPlatform : onlineSelfPlatformList) {
                List<BusProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(rule.getCustomId(), onlineSelfPlatform.getId()));
                BusProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), BusProductIncreaseStrategyRuleDTO.class);
                strategyRuleInfoDTO.setChannelFullName("线下代理渠道-" + onlineSelfPlatform.getChannelName());
                if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                    int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                    strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                }
                onlineRuleList.add(strategyRuleInfoDTO);
            }
            // 查询加幅策略-线上第三方
            QueryWrapper<BusProductChannelOnlineThirdPlatform> onlineThirdPlatformQueryWrapper = new QueryWrapper<>();
            onlineThirdPlatformQueryWrapper.eq("increase_strategy_id", busProductStrategy.getId());
            onlineThirdPlatformQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<BusProductChannelOnlineThirdPlatform> onlineThirdPlatformList = busProductChannelOnlineThirdPlatformMapper.selectList(onlineThirdPlatformQueryWrapper);
            // 查询加幅策略-线上第三方-组装所属加幅规则
            for (BusProductChannelOnlineThirdPlatform onlineThirdPlatform : onlineThirdPlatformList) {
                List<BusProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(rule.getCustomId(), onlineThirdPlatform.getId()));
                BusProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), BusProductIncreaseStrategyRuleDTO.class);
                strategyRuleInfoDTO.setChannelFullName("线下代理渠道-" + onlineThirdPlatform.getPlatformName());
                if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                    int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                    strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                }
                onlineRuleList.add(strategyRuleInfoDTO);
            }
            // 查询加幅策略-日期列表
            QueryWrapper<BusProductStrategyDate> strategyDateWrapper = new QueryWrapper<>();
            strategyDateWrapper.eq("increase_strategy_id", busProductStrategy.getId());
            List<BusProductStrategyDate> strategyDateList = busProductStrategyDateMapper.selectList(strategyDateWrapper);
            // 组装日期map
            for (BusProductStrategyDate strategyDate : strategyDateList) {
                long diffDay = strategyDate.getDateEnd().toEpochDay() - strategyDate.getDateBegin().toEpochDay();
                for (int i = 0; i <= diffDay; i++) {
                	LocalDate localDate = strategyDate.getDateBegin().plusDays(i);
                	
                	if (localDate.isBefore(beginDate)) {
                		continue;
                	}
                	if (!localDate.isBefore(endDate)) {
                		continue;
                	}
                	
                	BusProductDetailRuleDTO ruleDto = localDateListTreeMap.get(localDate);
                	if (ruleDto == null) {
                		ruleDto = new BusProductDetailRuleDTO();
                		localDateListTreeMap.put(localDate, ruleDto);
                	}
                	ruleDto.getOfflineRuleList().addAll(offlineRuleList);
                	ruleDto.getOnlineRuleList().addAll(onlineRuleList);
                }
            }
        }
        return localDateListTreeMap;
    }
    
    /**
     * 封装加幅相关
     * @param dto
     */
    private void packIncreaseStrategyRuleSoldNumber(BusProductIncreaseStrategyRuleDTO dto, BusProduct busProduct) {
    	if (dto == null) 
    		return;

    	//库存数
    	int saleableNumber = -1;

    	//有限制可售数量
    	if (Constants.SALE_TYPE_LIMIT.equals(dto.getSaleableType())) {
    		saleableNumber = Assist.defaultInt(dto.getSaleableNumber());
    	} else {  //无限制可售数量
    		saleableNumber = Assist.defaultInt(busProduct.getInventory());
    	}
    	
    	dto.setSaleableNumber(saleableNumber);
        
        
//    	if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
//    		int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
//    		strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
//    	}
	}

    /** 修改方法 */
    private void updateBatchSomeColumn(List list, BaseMapper mapper) {
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(list)) {
            for (Object o : list) {
                mapper.updateById(o);
            }
        }
    }


    /** 新增方法 */
    private void insertBatchSomeColumn(List list, BaseMapper mapper) {
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(list)) {
            for (Object o : list) {
                mapper.insert(o);
            }
        }
    }


    /**
     * 根据巴士产品id删除策略
     *
     * @param busProductIds 巴士产品id列表
     */
    private void deleteStrategyByAirportExpressId(List<Long> busProductIds) {
        if (CollectionUtils.isNotEmpty(busProductIds)) {
            List<Long> increaseStrategyIds = busProductStrategyMapper.selectIdListByBusProductIds(busProductIds);
            if (CollectionUtils.isNotEmpty(increaseStrategyIds)) {
                busProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineSelfCustom>().lambda().in(BusProductChannelOfflineSelfCustom::getIncreaseStrategyId, increaseStrategyIds));
                busProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<BusProductChannelOfflineAgentCustom>().lambda().in(BusProductChannelOfflineAgentCustom::getIncreaseStrategyId, increaseStrategyIds));
                busProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineSelfPlatform>().lambda().in(BusProductChannelOnlineSelfPlatform::getIncreaseStrategyId, increaseStrategyIds));
                busProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<BusProductChannelOnlineThirdPlatform>().lambda().in(BusProductChannelOnlineThirdPlatform::getIncreaseStrategyId, increaseStrategyIds));
                busProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<BusProductIncreaseStrategyRule>().lambda().in(BusProductIncreaseStrategyRule::getIncreaseStrategyId, increaseStrategyIds));
                busProductStrategyDateMapper.delete(new QueryWrapper<BusProductStrategyDate>().lambda().in(BusProductStrategyDate::getIncreaseStrategyId, increaseStrategyIds));
                busProductStrategyMapper.delete(new QueryWrapper<BusProductStrategy>().lambda().in(BusProductStrategy::getId, increaseStrategyIds));
            }
        }
    }


    /**
     * 加幅渠道和加幅时间校验
     *
     * @param busProductStrategyReqList
     * @return
     */
    private Result<Object> checkStrategyDateResult(List<BusProductStrategyReq> busProductStrategyReqList,boolean isSaveBatch) {

        // 四种渠道至少有一个在售校验
        boolean saleFlag = false;
        for (BusProductStrategyReq increaseStrategyReq : busProductStrategyReqList) {
            // 线下-自营渠道
            if (Arrays.asList(1, 2, 3).contains(increaseStrategyReq.getSelfSalePermissionType())) {
                saleFlag = true;
                continue;
            }
            if (Arrays.asList(1, 2, 3).contains(increaseStrategyReq.getAgentSalePermissionType())) {
                saleFlag = true;
                continue;
            }
            //线上-自营渠道
            if (CollectionUtils.isNotEmpty(increaseStrategyReq.getBusProductChannelOnlineSelfPlatformReqList())) {
                saleFlag = true;
                continue;
            }
            //线上-第三方渠道
            if (CollectionUtils.isNotEmpty(increaseStrategyReq.getBusProductChannelOnlineThirdPlatformReqList())) {
                saleFlag = true;
                continue;
            }
            if (!saleFlag) {
                return Result.failed(SystemError.AIREXPRESS_50003);
            }
            List<BusProductStrategyDateReq> airportExpressIncreaseStrategyDateReqList = increaseStrategyReq.getBusProductStrategyDateReqList();
            for (int index = 0; index < airportExpressIncreaseStrategyDateReqList.size(); index++) {
                BusProductStrategyDateReq strategyDateReq = airportExpressIncreaseStrategyDateReqList.get(index);
                if (isSaveBatch && strategyDateReq.getDateBegin().isBefore(LocalDate.now())) {
                    // 批量机场快线保存，开始日期不能小于当前日期校验
                    return Result.failed(SystemError.AIREXPRESS_50002);
                }
                if (index == 0 && strategyDateReq.getDateBegin().isAfter(strategyDateReq.getDateEnd())) {
                    // 开始日期必须小于结束日期校验
                    return Result.failed(SystemError.AIREXPRESS_50000);
                }
                for (int j = 0; j < airportExpressIncreaseStrategyDateReqList.size(); j++) {
                    BusProductStrategyDateReq strategyDateReqTemp = airportExpressIncreaseStrategyDateReqList.get(j);
                    if (index >= j) { // 自身和已经校验过的跳过日期校验
                        continue;
                    }
                    if (index == 0 && strategyDateReqTemp.getDateBegin().isAfter(strategyDateReqTemp.getDateEnd())) {
                        // 开始日期必须小于结束日期校验
                        return Result.failed(SystemError.AIREXPRESS_50000);
                    }
                    if (DateUtil.isDateRange(strategyDateReq.getDateBegin(), strategyDateReq.getDateEnd(), strategyDateReqTemp.getDateBegin(), strategyDateReqTemp.getDateEnd())) {
                        // 当前日期与其他日期区域重叠校验
                        return Result.failed(SystemError.AIREXPRESS_50001);
                    }
                }
            }
        }
        return null;
    }
    
    /**
     * 验证设置的最大可售数是否已超过库存数
     */
    private boolean validateSaleableNumber(BusProductIncreaseStrategyRule ruleEntity, Integer inventory) {
    	if (ruleEntity == null || SaleableType.SALEABLE_TYPE_1.getCode().equals(ruleEntity.getSaleableType()))
    		return true;
    	
    	Integer saleableNumber = ruleEntity.getSaleableNumber();
		if (saleableNumber == null || saleableNumber <= 0)
			return true;
		inventory = Assist.defaultInt(inventory);
		return saleableNumber <= inventory;
	}
}
